package com.odm.communitymanage.ui.mine.MySubmitRepair

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import com.blankj.utilcode.util.ResourceUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.core.bean.MyRepairData
import com.odm.communitymanage.util.GlideUtils

/**
 * @description: 我的报修 适配器
 * @author: ODM
 * @date: 2020/4/18
 */
class MyRepairAdapter(data :MutableList<MyRepairData>):
            BaseQuickAdapter<MyRepairData , BaseViewHolder>(R.layout.item_my_submit_repair, data ){



    override fun convert(holder: BaseViewHolder, item: MyRepairData) {
        holder.setText(R.id.tvDescription_MySubmitRepair , item.description)
        holder.setText(R.id.tvTime_MySubmitRepair , item.time)

        //根据类型 给类型 设置不同背景
        when(item.type){
            "水电维修"->{
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).background = context.getDrawable(R.drawable.shape_frame_blue_no_radius)
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).setTextColor(context.resources.getColor(R.color.repair_blue))
            }
            "消防通道"->{
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).background = context.getDrawable(R.drawable.shape_frame_red_no_radius)
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).setTextColor(context.resources.getColor(R.color.repair_red))

            }
            "电梯故障"->{
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).background = context.getDrawable(R.drawable.shape_frame_green_no_radius)
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).setTextColor(context.resources.getColor(R.color.repair_green))

            }
            "活动设施"->{
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).background = context.getDrawable(R.drawable.shape_frame_orange_no_raidus)
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).setTextColor(context.resources.getColor(R.color.repair_orange))

            }
            else -> {
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).background = context.getDrawable(R.drawable.shape_frame_blue_no_radius)
                holder.getView<TextView>(R.id.tvType_MySubmitRepair).setTextColor(context.resources.getColor(R.color.repair_blue))
            }
        }
        holder.setText(R.id.tvType_MySubmitRepair , item.type)
        //图片可能为 null
        item.imgs?.let {
            //加载图片
            val iv0 : ImageView = holder.getView<ImageView>(R.id.ivPhoto_0_MySubmitRepair)
            val iv1 : ImageView = holder.getView<ImageView>(R.id.ivPhoto_1_MySubmitRepair)
            val iv2 : ImageView = holder.getView<ImageView>(R.id.ivPhoto_2_MySubmitRepair)
            //按顺序加载图片
            val imageViews: Array<ImageView> = arrayOf(iv0, iv1, iv2)
            if (item.imgs.isEmpty()) {
                //无图片时全部清空遗留的viewHolder 且不可见
                for (iv in imageViews) {
                    Glide.with(context).clear(iv)
                    iv.visibility = View.GONE
                }
                return
            }
            //按顺序加载图片
            if (item.imgs.isNotEmpty()) {
                //根据图片数量限制 item的图片显示
                when(item.imgs.size){
                    1 -> {
                        imageViews[1].visibility = View.GONE
                        imageViews[2].visibility = View.GONE
                    }
                    2 -> {
                        imageViews[2].visibility = View.GONE
                    }
                }

                for (index in item.imgs.indices) {
                    val url = item.imgs[index]
                    GlideUtils.getInstance()
                            .loadImageUrl(context, url, imageViews[index])

/*                    val tag = imageViews[index].getTag(R.id.recyclerview_image_url)
                    if (tag != null && tag != url) {
                        Glide.with(context).clear(imageViews[index])
                        imageViews[index].visibility = View.GONE
                    } else {
                        if((index > 0 && imageViews[index-1].visibility == View.VISIBLE)||index == 0 ){
                            GlideUtils.getInstance()
                                .loadImageUrl(context, url, imageViews[index])
                            imageViews[index].visibility = View.VISIBLE
                            imageViews[index].setTag(R.id.recyclerview_image_url, url)
                        }
                    }*/
                }
            }
/*            if(iv0.visibility != View.VISIBLE && iv1.visibility == View.VISIBLE){
                iv1.visibility =View.GONE
                iv2.visibility = View.GONE
                if (item.imgs.isNotEmpty()) {
                    iv0.visibility = View.VISIBLE
                    val url = item.imgs[0]
                    GlideUtils.getInstance()
                        .loadImageUrl(context, url, iv0)
                    iv0.visibility = View.VISIBLE
                    iv0.setTag(R.id.recyclerview_image_url, url)
                }
            }*/
        }
    }
}

    class MyRepairDiffCallback(private val oldList: List<MyRepairData> ?, private val newList: List<MyRepairData> ?) :
        DiffUtil.Callback() {

        override fun getOldListSize(): Int {
            return oldList?.size ?: 0
        }

        override fun getNewListSize(): Int {
            return newList?.size ?: 0
        }

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList?.get(oldItemPosition)?.javaClass == newList?.get(newItemPosition)?.javaClass
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return  oldList?.get(oldItemPosition)?.id == newList?.get(newItemPosition)?.id &&
                    oldList?.get(oldItemPosition)?.imgs == newList?.get(newItemPosition)?.imgs
        }
    }